package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentCheckDetail;
import com.mxpio.erp.common.equipment.service.EquipmentCheckDetailService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @Description: 点检标准明细
* @Author: wxy
* @Date:   2021-09-15
* @Version: V1.0
*/
@Tag(name = "EquipmentCheckDetailController", description = "点检标准明细")
@RestController
@RequestMapping("/erp/equipment/spotcheck/tpmCheckDetail")
@Slf4j
public class EquipmentCheckDetailController {

   @Autowired
   private EquipmentCheckDetailService equipmentCheckDetailService;

   /**
    * 通过主表ID查询
    * @return
    */
   @GetMapping(value = "/page")
   @Operation(summary = "点巡检标准明细-通过主表ID查询", description = "点巡检标准明细-通过主表ID查询", method = "GET")
   public Result<?> listTpmCheckDetailByMainId(@Parameter(description="查询参数") Criteria criteria,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {
       Pageable page = PageRequest.of(pageNo-1, pageSize);
       Page<EquipmentCheckDetail> pageList = equipmentCheckDetailService.listPage(EquipmentCheckDetail.class,page, criteria);
       return Result.OK(pageList);
   }

   /**
    * 添加
    * @param equipmentCheckDetail
    * @return
    */
   @PostMapping(value = "/add")
   @Operation(summary = "点巡检标准明细-添加", description = "点巡检标准明细-添加", method = "POST")
   public Result<?> addTpmCheckDetail(@RequestBody EquipmentCheckDetail equipmentCheckDetail) {
       equipmentCheckDetailService.saveFromParams(equipmentCheckDetail);
       return Result.OK("添加成功！");
   }

   /**
    * 编辑
    * @param equipmentCheckDetail
    * @return
    */
   @Operation(summary = "点巡检标准明细-编辑", description = "点巡检标准明细-编辑", method = "PUT")
   @PutMapping(value = "/edit")
   public Result<?> editTpmCheckDetail(@RequestBody EquipmentCheckDetail equipmentCheckDetail) {
       equipmentCheckDetailService.editFromParams(equipmentCheckDetail);
       return Result.OK("编辑成功!");
   }

   /**
    * 通过id删除
    * @param id
    * @return
    */
   @DeleteMapping(value = "/delete/{id}")
   @Operation(summary = "点巡检标准明细-通过id删除", description = "点巡检标准明细-通过id删除", method = "DELETE")
   public Result<?> deleteTpmCheckDetail(@PathVariable(name="id",required=true) String id) {
      equipmentCheckDetailService.delete(EquipmentCheckDetail.class,id);
       return Result.OK();
   }

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @DeleteMapping(value = "/deleteBatch")
   @Operation(summary = "点巡检标准明细-批量删除", description = "点巡检标准明细-批量删除", method = "DELETE")
   public Result<?> deleteBatchTpmCheckDetail(@RequestParam(name="ids",required=true) String ids) {
       Criteria c = Criteria.create().addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
       equipmentCheckDetailService.removeBatch(EquipmentCheckDetail.class,c);
       return Result.OK("批量删除成功!");
   }

//   /**
//    * 导出
//    * @return
//    */
//   @RequestMapping(value = "/exportTpmCheckDetail")
//   public ModelAndView exportTpmCheckDetail(HttpServletRequest request, TpmCheckDetail equipmentCheckDetail) {
//        // Step.1 组装查询条件
//        QueryWrapper<TpmCheckDetail> queryWrapper = QueryGenerator.initQueryWrapper(equipmentCheckDetail, request.getParameterMap());
//        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//
//        // Step.2 获取导出数据
//        List<TpmCheckDetail> pageList = equipmentCheckDetailService.list(queryWrapper);
//        List<TpmCheckDetail> exportList = null;
//
//        // 过滤选中数据
//        String selections = request.getParameter("selections");
//        if (oConvertUtils.isNotEmpty(selections)) {
//            List<String> selectionList = Arrays.asList(selections.split(","));
//            exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
//        } else {
//            exportList = pageList;
//        }
//
//        // Step.3 AutoPoi 导出Excel
//        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//        mv.addObject(NormalExcelConstants.FILE_NAME, "点巡检标准明细"); //此处设置的filename无效 ,前端会重更新设置一下
//        mv.addObject(NormalExcelConstants.CLASS, TpmCheckDetail.class);
//        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("点巡检标准明细报表", "导出人:" + sysUser.getRealname(), "点巡检标准明细"));
//        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
//        return mv;
//   }
//
//   /**
//    * 导入
//    * @return
//    */
//   @RequestMapping(value = "/importTpmCheckDetail/{mainId}")
//   public Result<?> importTpmCheckDetail(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//            MultipartFile file = entity.getValue();// 获取上传文件对象
//            ImportParams params = new ImportParams();
//            params.setTitleRows(2);
//            params.setHeadRows(1);
//            params.setNeedSave(true);
//            try {
//                List<TpmCheckDetail> list = ExcelImportUtil.importExcel(file.getInputStream(), TpmCheckDetail.class, params);
//                for (TpmCheckDetail temp : list) {
//                   temp.setCategoryId(mainId);
//                }
//                long start = System.currentTimeMillis();
//                equipmentCheckDetailService.saveBatch(list);
//                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
//                return Result.OK("文件导入成功！数据行数：" + list.size());
//            } catch (Exception e) {
//                log.error(e.getMessage(), e);
//                return Result.error("文件导入失败:" + e.getMessage());
//            } finally {
//                try {
//                    file.getInputStream().close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return Result.error("文件导入失败！");
//   }

}
